#ifndef QMLPLOT_H
#define QMLPLOT_H

#include <QtQuick>
#include "qcustomplot.h"
class QCustomPlot;
class QCPAbstractPlottable;

class DDuiBasePlotItem : public QQuickPaintedItem
{
    Q_OBJECT
public:
    DDuiBasePlotItem( QQuickItem* parent = 0 );
    virtual ~DDuiBasePlotItem();
    void paint( QPainter* painter );
    virtual void initCustomPlotUI(){}

    QCustomPlot *getPlot();
protected:
    virtual void hoverMoveEvent(QHoverEvent *event);
    virtual void mousePressEvent( QMouseEvent* event );
    virtual void mouseReleaseEvent( QMouseEvent* event );
    virtual void mouseMoveEvent( QMouseEvent* event );
    virtual void mouseDoubleClickEvent( QMouseEvent* event );
    virtual void wheelEvent( QWheelEvent *event );

    void routeMouseEvents( QMouseEvent* event );
    void routeWheelEvents( QWheelEvent* event );
public slots:
    void graphClicked( QCPAbstractPlottable* plottable );
    void onCustomReplot();
    void updateCustomPlotSize();

protected:
    QCPItemTracer* tracer = nullptr;

private:
    QCustomPlot*         m_CustomPlot;
};

class CustomPlotItem : public DDuiBasePlotItem
{
    Q_OBJECT
public:
    CustomPlotItem( QQuickItem* parent = 0 );
    virtual ~CustomPlotItem();
    Q_INVOKABLE void initCustomPlot();
protected:
    virtual void timerEvent(QTimerEvent *event);
private:
    int                  m_timerId;
};

class MyAxisTickerText : public QCPAxisTickerText
{
protected:
    virtual QVector<double> createTickVector(double tickStep, const QCPRange &range) Q_DECL_OVERRIDE
    {
        Q_UNUSED(tickStep)
        QVector<double> result;
        if (mTicks.isEmpty())
            return result;

        auto start = mTicks.lowerBound(range.lower);
        auto end = mTicks.upperBound(range.upper);
        if (start != mTicks.constBegin()) --start;
        if (end != mTicks.constEnd()) ++end;

        int count = cleanMantissa(std::distance(start, end) / double(mTickCount + 1e-10));

        auto it = start;
        while (it != end) {
            result.append(it.key());
            int step = count;
            if (step == 0) ++it;
            while (--step >= 0 && it != end)
                ++it;
        }

        return result;
    }
};

class MarketCustomPlotItem : public DDuiBasePlotItem
{
    Q_OBJECT
public:
    MarketCustomPlotItem( QQuickItem* parent = 0 );
    virtual ~MarketCustomPlotItem();
    Q_INVOKABLE void initCustomPlot();
    virtual void hoverMoveEvent(QHoverEvent *event);
private:
};


class CustomPlotBarItem : public DDuiBasePlotItem
{
    Q_OBJECT
    Q_PROPERTY(QColor barColor READ barColor WRITE setBarColor NOTIFY barColorChanged)
public:
    CustomPlotBarItem( QQuickItem* parent = 0 );
    virtual ~CustomPlotBarItem();    
    Q_INVOKABLE void initCustomPlot();
    QColor barColor();
    void setBarColor(const QColor &color);
signals:
    void barColorChanged(const QColor &color);
private:
    QColor mBarColor{Qt::blue};
};

class CustomKLine : public DDuiBasePlotItem{
  Q_OBJECT
public:
    explicit CustomKLine(QQuickItem* parent = 0);
    Q_INVOKABLE void initCustomPlot();
};

class CustomColorMap : public DDuiBasePlotItem{
  Q_OBJECT
public:
    explicit CustomColorMap(QQuickItem* parent = 0);
    Q_INVOKABLE void initCustomPlot();
};

class CustomBarChart: public DDuiBasePlotItem
{
  Q_OBJECT
public:
    explicit CustomBarChart(QQuickItem *parent=0){}

    Q_INVOKABLE void iniCustomPlot();
};
#endif // QMLPLOT_H
